class Solution:
    def videoStitching(self, clips: List[List[int]], T: int) -> int:
        left=0
        right=T
        def findstitch(left,right):
            count=0
            templeft=left
            tempright=right
            for i in clips:
                if i[0]<=left and i[1]>templeft:
                    templeft=i[1]
                    if templeft>=right:
                        return count+1
                if i[1]>=right and i[0]<tempright:
                    tempright=i[0]
            if templeft==left or tempright==right:
                return -1
            if templeft>=tempright:
                return count+2
            result=findstitch(templeft,tempright)
            if result==-1:
                return -1
            else:
                return count+result+2
        result=findstitch(0,T)
        if result==-1:
            return -1
        else:
            return result

